Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding trait example into documentation. #75

Closed
wants to merge 2 commits into from

Conversation

vrkansagara
Copy link

Q A
Documentation yes
Bugfix no
BC Break no
New Feature no
RFC no
QA no

Description

@vrkansagara
Copy link
Author

Ping @michalbundyra

@Ocramius
Copy link
Member

4091991 should probably not be part of this patch: was the patch started from this branch?

@vrkansagara
Copy link
Author

yes @Ocramius , I think there is some webhook executed by @michalbundyra which add 4091991

@Ocramius
Copy link
Member

@vrkansagara nay, that commit is in master and 3.4.x since a while - I think the edit was simply done starting from the wrong branch. No big deal, but requires manual merge, rather than merge from here.

@froschdesign froschdesign linked an issue Apr 14, 2021 that may be closed by this pull request
Copy link
Member

@froschdesign froschdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, too much was copied from a previous section. Some corrections are needed.

Comment on lines +377 to +378
`Laminas\Code\Generator\FileGenerator` can be used to generate the contents of a *PHP* file. You can
include classes as well as arbitrary content body.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two sentences are copied from the section "Generating PHP files" and it does not the describe the generating of a trait.

Comment on lines +380 to +391
```php
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\TraitGenerator;

$traitclass = new TraitGenerator();
$traitclass->addMethod('MethodOne');
$traitclass->addConstant('VERSION','1.0.0');

$traitFile = new Laminas\Code\Generator\FileGenerator();
$traitFile->setClass($traitclass);

```
Copy link
Member

@froschdesign froschdesign Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code example can be shortened to the essentials.

Suggested change
```php
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\TraitGenerator;
$traitclass = new TraitGenerator();
$traitclass->addMethod('MethodOne');
$traitclass->addConstant('VERSION','1.0.0');
$traitFile = new Laminas\Code\Generator\FileGenerator();
$traitFile->setClass($traitclass);
```
```php
$trait = new Laminas\Code\Generator\TraitGenerator();
$trait->setName('TranslatorAwareTrait');
$trait->addProperty(
'translator', // Name
null, // Default value
Laminas\Code\Generator\PropertyGenerator::FLAG_PRIVATE // Flags
);
$trait->addMethod(
'getTranslator', // Name
[], // Parameters
Laminas\Code\Generator\PropertyGenerator::FLAG_PUBLIC, // Flags
'return $this->translator;' // Body
);
echo $trait->generate();

Copy link

@RSully RSully Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the use statements could still be handy - though I admit I don't know how the rest of the documentation is structured and if there's a standard or not.

Edit: never-mind ignore me :) - I see the classes are fully qualified

Comment on lines +393 to +415
Calling `generate()` will generate the code -- but not write it to a file. You will need to capture
the contents and write them to a file yourself. As an example:

```php
$code = $traitFile->generate();
file_put_contents('Trait.php', $traitclass);
```

The above will generate the following file:

```php
<?php

trait
{
public const VERSION = '1.0.0';

public function MethodOne()
{
}
}

```
Copy link
Member

@froschdesign froschdesign Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Calling `generate()` will generate the code -- but not write it to a file. You will need to capture
the contents and write them to a file yourself. As an example:
```php
$code = $traitFile->generate();
file_put_contents('Trait.php', $traitclass);
```
The above will generate the following file:
```php
<?php
trait
{
public const VERSION = '1.0.0';
public function MethodOne()
{
}
}
```
The code generator produces the following trait:
```php
trait TranslatorAwareTrait
{
private $translator = null;
public function getTranslator()
{
return $this->translator;
}
}

@RSully
Copy link

RSully commented Apr 15, 2021

Is it possible to add code to the body of a generated trait method? That would seem important and is missing from these examples, and part of why I made my initial request.

@vrkansagara
Copy link
Author

Is it possible to add code to the body of a generated trait method? That would seem important and is missing from these examples, and part of why I made my initial request.

@RSully I have already explained here 847be56#diff-3d8f5388de37ae1d660bf9198127eb4e4a7d0796e5cd53fab4d602293b388371R406

@RSully
Copy link

RSully commented Apr 15, 2021

That doesn't address my question. For example, I am asking to show something like this:

trait HelloWorld {
    private $greeting = 'Hello World';

    public function sayHello() {
        echo $this->greeting;
    }
}

Also, according to a quick test in PHP 8, traits cannot have constants - so your example's generated code doesn't compile.

Fatal error: Traits cannot have constants in [...]

@froschdesign
Copy link
Member

@RSully

Is it possible to add code to the body of a generated trait method?

I have updated the example from above.

@Ocramius Ocramius added the Won't Fix This will not be worked on label Nov 18, 2022
@Ocramius Ocramius self-assigned this Nov 18, 2022
@Ocramius
Copy link
Member

Houskeeping is here, closing :)

@Ocramius Ocramius closed this Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Examples for Trait generator
5 participants